package com.blue.base.security.filters;

import com.blue.base.security.config.SecurityAuthConfig;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;

import javax.servlet.http.HttpServletRequest;

/**
 * 重写基础用户名、密码认证
 *
 * @author liulei
 * @version 1.0
 */
public abstract class AbstractUsernamePasswordFilter extends AbstractAuthenticationProcessingFilter {

    private SecurityAuthConfig authConfig;

    protected AbstractUsernamePasswordFilter(SecurityAuthConfig authConfig) {
        // 定义登录处理地址
        super(authConfig.getLoginUrl());
        this.authConfig = authConfig;
    }

    protected String obtainUsername(HttpServletRequest request) {
        return request.getParameter(authConfig.getUsername());
    }

    protected String obtainPassword(HttpServletRequest request) {
        return request.getParameter(authConfig.getPassword());
    }

    protected void setDetails(HttpServletRequest request, UsernamePasswordAuthenticationToken authRequest) {
        authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
    }

}
